local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname)

if not throwables then return end

local item_name = "default:snow"
local projectile_name = modname .. ':' .. item_name:match(".*:([^:]*)$") .. "_thrown"

if not minetest.registered_items[item_name] then return end

-- Add throwable capacity to default:snow
local snowball = throwables:add(item_name, {
    description = S("Snowball"),
    projectile_name = projectile_name,
    inventory_image = "default_snowball.png",
    initial_properties = {
        hp_max = 1,
        physical = true,
        collide_with_objects = true,
        collisionbox = {-0.2, -0.2, -0.2, 0.2, 0.2, 0.2},
        visual = "wielditem",
        visual_size = {x = 0.5, y = 0.5},
        textures = {"default:snow"},
        pointable = false,
        speed = 20,
        gravity = 9.8,
        lifetime = 5,
        damage = 2,
    },
})

-- Callback functions
function on_hit_entity(self, entity)
    entity:set_hp(entity:get_hp() - self.initial_properties.damage)
    self.object:remove()
end
function on_hit_player(self, player)
    on_hit_entity(self, player)
end

function on_hit_node(self, node)
    local node_to_place = minetest.registered_nodes[item_name] or minetest.registered_items[item_name]
    local pos = self.object:get_pos()
    minetest.place_node(pos, node_to_place)
    self.object:remove()
end

-- Register callbacks
snowball:on_hit_player(on_hit_player)
snowball:on_hit_entity(on_hit_entity)
snowball:on_hit_node(on_hit_node)
